package com.yang.controller;

import com.alibaba.fastjson.JSON;
import com.yang.pojo.*;
import com.yang.pojo.returnPojo.Return_Comment;
import com.yang.pojo.returnPojo.Return_User;
import com.yang.service.UserITService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/interactive")
@CrossOrigin
public class UserITController {
    @Autowired
    @Qualifier("UserITServiceImpl")
    private UserITService userITService;

    //获取用户对指定歌手关注信息
    @RequestMapping("/userFollowSonger")
    public String getUserFollowSonger(@RequestBody String userITObject){
        User_Follow_Songer userFollowObject = JSON.parseObject(userITObject,User_Follow_Songer.class);
        User_Follow_Songer userFollowSonger = userITService.getFollowSonger(userFollowObject);
        if(userFollowSonger == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户关注歌手
    @RequestMapping("/followSonger")
    public String followSonger(@RequestBody String userITObject){
        User_Follow_Songer userFollowObject = JSON.parseObject(userITObject,User_Follow_Songer.class);
        User_Follow_Songer userFollowSonger = userITService.getFollowSonger(userFollowObject);
        if(userFollowSonger == null){
            userITService.followSonger(userFollowObject);
            return "true";
        } else {
            userITService.deFollowSonger(userFollowObject);
            return "false";
        }
    }

    //获取用户对指定用户关注信息
    @RequestMapping("/userFollowUser")
    public String getUserFollowUserer(@RequestBody String userITObject){
        User_Follow_User userFollowObject = JSON.parseObject(userITObject,User_Follow_User.class);
        User_Follow_User userFollowUser = userITService.getFollowUser(userFollowObject);
        if(userFollowUser == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户关注用户
    @RequestMapping("/followUser")
    public String followUser(@RequestBody String userITObject){
        User_Follow_User userFollowObject = JSON.parseObject(userITObject,User_Follow_User.class);
        User_Follow_User userFollowUser = userITService.getFollowUser(userFollowObject);
        if(userFollowUser == null){
            userITService.followUser(userFollowObject);
            return "true";
        } else {
            userITService.deFollowUser(userFollowObject);
            return "false";
        }
    }

    //获取用户对指定专辑的收藏信息
    @RequestMapping("/userCollectAlbum")
    public String getUserCollectAlbum(@RequestBody String userITObject){
        User_Collect_Album userCollectObject = JSON.parseObject(userITObject,User_Collect_Album.class);
        User_Collect_Album userCollectAlbum = userITService.getCollectAlbum(userCollectObject);
        if(userCollectAlbum == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户收藏专辑
    @RequestMapping("/collectAlbum")
    public String collectAlbum(@RequestBody String userITObject){
        User_Collect_Album userCollectObject = JSON.parseObject(userITObject,User_Collect_Album.class);
        User_Collect_Album userCollectAlbum = userITService.getCollectAlbum(userCollectObject);
        if(userCollectAlbum == null){
            userITService.collectAlbum(userCollectObject);
            return "true";
        } else {
            userITService.deCollectAlbum(userCollectObject);
            return "false";
        }
    }

    //获取用户对指定歌单的收藏信息
    @RequestMapping("/userCollectSongList")
    public String getUserCollectSongList(@RequestBody String userITObject){
        User_Collect_SongList userCollectObject = JSON.parseObject(userITObject,User_Collect_SongList.class);
        User_Collect_SongList userCollectSongList = userITService.getCollectSongList(userCollectObject);
        if(userCollectSongList == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户收藏歌单
    @RequestMapping("/collectSongList")
    public String collectSongList(@RequestBody String userITObject){
        User_Collect_SongList userCollectObject = JSON.parseObject(userITObject,User_Collect_SongList.class);
        User_Collect_SongList userCollectSongList = userITService.getCollectSongList(userCollectObject);
        if(userCollectSongList == null){
            userITService.collectSongList(userCollectObject);
            return "true";
        } else {
            userITService.deCollectSongList(userCollectObject);
            return "false";
        }
    }

    //获取用户对指定MV的收藏信息
    @RequestMapping("/userCollectMV")
    public String getUserCollectMV(@RequestBody String userITObject){
        User_Collect_MV userCollectObject = JSON.parseObject(userITObject,User_Collect_MV.class);
        User_Collect_MV userCollectMV = userITService.getCollectMV(userCollectObject);
        if(userCollectMV == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户收藏MV
    @RequestMapping("/collectMV")
    public String collectMV(@RequestBody String userITObject){
        User_Collect_MV userCollectObject = JSON.parseObject(userITObject,User_Collect_MV.class);
        User_Collect_MV userCollectMV = userITService.getCollectMV(userCollectObject);
        if(userCollectMV == null){
            userITService.collectMV(userCollectObject);
            return "true";
        } else {
            userITService.deCollectMV(userCollectObject);
            return "false";
        }
    }

    //获取用户的点赞信息
    @RequestMapping("/userLiked")
    public String getUserLiked(@RequestBody String userITObject){
        User_Liked userLikedObject = JSON.parseObject(userITObject,User_Liked.class);
        User_Liked userLiked = userITService.getLiked(userLikedObject);
        if(userLiked == null){
            return "false";
        } else {
            return "true";
        }
    }

    //用户点赞
    @RequestMapping("/liked")
    public String liked(@RequestBody String userITObject){
        User_Liked userLikedObject = JSON.parseObject(userITObject,User_Liked.class);
        User_Liked userLiked = userITService.getLiked(userLikedObject);
        if(userLiked == null){
            userITService.liked(userLikedObject);
            return "true";
        } else {
            userITService.deLiked(userLikedObject);
            return "false";
        }
    }

    //评论
    @RequestMapping("/comment")
    public String comment(@RequestBody String userITObject){
        User_Comment userCommentObject = JSON.parseObject(userITObject,User_Comment.class);
        userITService.saveComment(userCommentObject);
        return "200";
    }

    //获取指定作品评论
    @RequestMapping("/getComment")
    public String getComment(@RequestBody String userITObject){
        User_Comment userCommentObject = JSON.parseObject(userITObject,User_Comment.class);
        List<User_Comment> userCommentList = userITService.getComment(userCommentObject);
        Return_Comment[] returnComments = new Return_Comment[userCommentList.size()];
        int i = 0;
        for (User_Comment userComment : userCommentList) {
            Return_User returnUser = new Return_User();
            returnUser.setUserId(userComment.getUserId());
            returnUser.setAvatarUrl(userComment.getAvatarUrl() + "/" +userComment.getUserImgName());
            returnUser.setNickname(userComment.getNickname());

            Return_Comment returnComment = new Return_Comment();
            returnComment.setCommentId(userComment.getCommentId());
            returnComment.setContent(userComment.getContent());
            returnComment.setLikedCount(userComment.getLikedCount());
            returnComment.setTime(userComment.getTime());
            returnComment.setUser(returnUser);

            returnComments[i] = returnComment;
            i++;
        }
        return JSON.toJSONString(returnComments);
    }
}
